﻿using Jinjia.Approve.Entity;
using Jinjia.Approve.IRepository;
using Jinjia.Core.BaseInfrastructure.StorageModel;
using Jinjia.Core.BaseInfrastructure.StorageModel.Impl;
using Jinjia.Core.UnitOfWork.UnitOfWorkComponent;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System;
using Jinjia.Approve.Entity.Entity;
using Jinjia.Approve.Model;
using SqlSugar;

namespace Jinjia.Approve.Repository
{
    /// <summary>
    /// 审批流模板远程接口维护
    ///</summary>
    public class JJFlowTemplateRemoteInterfaceRepository : BaseRepository<JJFlowTemplateRemoteInterface>, IJJFlowTemplateRemoteInterfaceRepository
    {
        private readonly IUnitOfWork _unitOfWork;

        public JJFlowTemplateRemoteInterfaceRepository(IUnitOfWork unitOfWork, IInitUserInfo initUserInfo) : base(unitOfWork, initUserInfo)
        {
            _unitOfWork = unitOfWork;
        }

        /// <summary>
        /// 获取完整的接口信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<JJFlowTemplateRemoteInterfaceResponse> GetFullFirstAsync(Expression<Func<JJFlowTemplateRemoteInterface, bool>> predicate)
        {
            return await _db.Queryable<JJFlowTemplateRemoteInterface>()
                 .LeftJoin<erpa_base_tbl>((a, b) => a.BaseAddressCode == b.name)
                 .Where(predicate)
                 .Where((a, b) => b.code_type == "JJFlowTemplateRemoteInterface")
                 .Select((a, b) => new JJFlowTemplateRemoteInterfaceResponse
                 {
                     Address = a.Address,
                     BaseAddressCode = a.BaseAddressCode,
                     InterfaceCode = a.InterfaceCode,
                     IncludeContentFlag = a.IncludeContentFlag,
                     Method = a.Method,
                     id = a.id,
                     BaseAddress = b.value,
                 }).FirstAsync();
        }
    }
}